package com.itnetwork.DaoImp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.itnetwork.Entity.Category;
@Component("categoryDaoImp")
public class CategoryDaoImp {
	private  SessionFactory sessionFactory;
	
	public List<Category> getSecondCategoryList()
	{
		
		List<Category> categorys=new ArrayList<Category>();
		Session session = sessionFactory.getCurrentSession();
		//categorys=hibernateTemplate.find("from Category c where c.cparent!=null");
		Query query=session.createQuery("from Category c where c.cparent!=null");
		categorys = query.list();
		System.out.println("******************************************************************");
		return categorys;
	}

	public List<Category> getCategoryList()
	{
		List<Category> categorys=new ArrayList<Category>();
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createQuery("from Category");
		//categorys=hibernateTemplate.find("from Category");
		categorys=query.list();
		return categorys;
	}
	
	public List<Object[]>  getCategoryComboboxModel()
	{
		System.out.println("category dao comboboxmodel start..");
		List<Object[]> categories=new ArrayList<Object[]>();
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createQuery("select c.cid,  c.ctitle from Category c where c.cparent!=null");
		//categorys=hibernateTemplate.find("from Category");
		categories=query.list();
		//=hibernateTemplate.find("select c.cid,  c.ctitle from Category c where c.cparent!=null");
		/*for(Object[] o : categories) {
			System.out.println(o[0] + "-" + o[1]);，
		}*/
		return categories;
	}
	public void saveCategory(Category category)
	{
		//hibernateTemplate.save(category);
		Session session = sessionFactory.getCurrentSession();//.getCurrentSession();
		session.save(category);
	}
	
	public SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
